#
# Top level makefile for building "ijsgutenprint" on OpenWRT
#

include $(TOPDIR)/rules.mk

PKG_NAME:=ijsgutenprint
PKG_VERSION:=1.0.0
PKG_RELEASE:=1

export LDFLAGS:=-s

include $(INCLUDE_DIR)/package.mk

define Package/ijsgutenprint
	SECTION:=net
	CATEGORY:=Network
	TITLE:=ijsgutenprint -- Gutenprint IJS backend
endef

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) -a libijs gutenprint/* $(PKG_BUILD_DIR)
endef

define Build/Configure
	# Gutenprint depends on libijs. Build it statically and put the
	# "ijs-config" binary on the PATH, where the "configure" script can
	# find it.
	(cd $(PKG_BUILD_DIR)/libijs && \
	 $(TARGET_CONFIGURE_OPTS) \
	   ./configure --host=$(REAL_GNU_TARGET_NAME) \
	               --build=$(GNU_HOST_NAME) \
	               --prefix=$(PKG_BUILD_DIR)/libijs/install && \
	 $(MAKE) install && \
	 $(RM) -rf $(PKG_BUILD_DIR)/host $(PKG_BUILD_DIR).host && \
	 $(CP) -a $(PKG_BUILD_DIR) $(PKG_BUILD_DIR).host && \
	 mv $(PKG_BUILD_DIR).host $(PKG_BUILD_DIR)/host)
	# Gutenprint isn't normally intended to be cross-compiled. It needs to
	# run "extract-strings" at build-time. And this binary needs to have
	# access to "libgutenprint.so". This forces us to build Gutenprint
	# both natively and a cross-compiled code, and to make the native
	# "extract-strings" binary available to both build systems.
	(cd $(PKG_BUILD_DIR)/host && \
	 ./configure && \
	 for i in `find $(PKG_BUILD_DIR) -name Makefile`; do \
	   sed -i '/PACKAGE_COPYRIGHT =/,+13s/[ ]*[\]*$$$$/ \\/' "$$$$i"; \
	 done && \
	 $(MAKE) -C src/xml extract-strings)
	# Configure Gutenprint for cross-compiling
	(cd $(PKG_BUILD_DIR) && \
	 $(TARGET_CONFIGURE_OPTS) \
	   PATH=$(PKG_BUILD_DIR)/libijs/install/bin:$$$$PATH \
	   ./configure --disable-nls \
	               --without-cups \
	               --without-libiconv-prefix \
	               --without-readline \
	               --without-foomatic \
	               --without-foomatic3 \
	               --host=$(REAL_GNU_TARGET_NAME) \
	               --build=$(GNU_HOST_NAME) \
	               --program-prefix="" \
	               --program-suffix="" \
	               --prefix=/usr \
	               --exec-prefix=/usr \
	               --bindir=/usr/bin \
	               --sbindir=/usr/bin \
	               --libexecdir=/usr/lib \
	               --sysconfdir=/etc && \
	 for i in `find $(PKG_BUILD_DIR) -name Makefile`; do \
	   sed -i '/PACKAGE_COPYRIGHT =/,+13s/[ ]*[\]*$$$$/ \\/' "$$$$i"; \
	 done && \
	 $(MAKE) -C src/xml extract-strings && \
	 echo '\#!/bin/sh' >src/xml/extract-strings && \
	 echo 'exec $(PKG_BUILD_DIR)/host/src/xml/extract-strings "$$$$@"' \
	   >>src/xml/extract-strings)
endef

define Build/Compile
	$(RM) -rf $(PKG_BUILD_DIR)/install
	$(MAKE) -C $(PKG_BUILD_DIR)
	$(MAKE) -C $(PKG_BUILD_DIR) install DESTDIR=$(PKG_BUILD_DIR)/install
endef

TARGET_CFLAGS += $(FPIC)

define Package/ijsgutenprint/install
	$(INSTALL_DIR) $(1)/usr/bin $(1)/usr/lib $(1)/usr/share/gutenprint
	$(CP) $(PKG_BUILD_DIR)/install/usr/bin/ijsgutenprint.* \
	      $(1)/usr/bin/ijsgutenprint
	$(CP) $(PKG_BUILD_DIR)/install/usr/lib/libgutenprint.so* $(1)/usr/lib
	$(CP) $(PKG_BUILD_DIR)/install/usr/share/gutenprint/[0-9]*.[0-9]* \
	      $(1)/usr/share/gutenprint
	$(TARGET_CROSS)strip $(1)/usr/bin/* $(1)/usr/lib/*.so.*.*
endef

$(eval $(call BuildPackage,ijsgutenprint))
